#include "data_chart.h"
#include "ui_data_chart.h"

#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include "power_board.h"


QLineSeries *series1 = new QLineSeries();
QLineSeries *series2 = new QLineSeries();
QLineSeries *series3 = new QLineSeries();
QLineSeries *series4 = new QLineSeries();
QValueAxis *axisX = new QValueAxis();
QValueAxis *axisY = new QValueAxis();

float MAX_Y = 0;
static qreal a = 0;
unsigned char clear_flag;


Data_chart::Data_chart(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Data_chart)
{
    ui->setupUi(this);

    // 创建图表和数据系列


    // series1->setPointsVisible(true);

    QChart *chart = new QChart();   //创建一个图表容器对象（QChart），用于管理数据系列、坐标轴、标题等图表元素。
    chart->addSeries(series1);
    chart->addSeries(series2);
    chart->addSeries(series3);
    chart->addSeries(series4);
    chart->setTitle("功率曲线");
    // 创建坐标轴并添加到图表

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);
    // 将系列附加到坐标轴（替代已弃用的 setAxisX/setAxisY）
    series1->attachAxis(axisX);
    series1->attachAxis(axisY);
    series2->attachAxis(axisX);
    series2->attachAxis(axisY);
    series3->attachAxis(axisX);
    series3->attachAxis(axisY);
    series4->attachAxis(axisX);
    series4->attachAxis(axisY);
    series1->setName("1通道");
    series2->setName("2通道");
    series3->setName("3通道");
    series4->setName("4通道");




    // 设置初始范围
    axisX->setRange(0, 5);
    axisY->setRange(-5, 5);
    axisX->setTickCount(11);  // 创建 10 条垂直网格线
    axisY->setTickCount(11);  // 创建 10 条水平网格线
    axisX->setTitleText("时间");
    axisY->setTitleText("功率（W）");

    // 将图表设置到提升后的 QChartView 中

    // 使用自定义的ChartView显示图表
    CustomChartView *chartView = new CustomChartView(chart);
    chartView->setParent(nullptr); // 可根据你的主窗口设置父对象，例如：chartView->setParent(mainWindow);
    chartView->resize(600, 500);
    chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->chart_Layout->addWidget(chartView);
    chartView->show();

}


Data_chart::~Data_chart()
{
    delete ui;
}
void Data_chart::on_start_clicked()
{
    // if(timer->isActive())
    // {
    //     timer->stop();
    //     ui->start->setText("开始");
    // }
    // else
    // {
    //     timer->start(100);
    //     ui->start->setText("停止");
    // }
}

void Data_chart::on_clear_line_clicked()
{
    series1->clear();
    series2->clear();
    series3->clear();
    series4->clear();
    clear_flag = 1;
    a = 0;
}
void Data_chart::on_new_line_clicked()
{

    // QLineSeries *series_new = new QLineSeries();
    // series_new->setPointsVisible(true);
    // QChart *chart_new = new QChart();   //创建一个图表容器对象（QChart），用于管理数据系列、坐标轴、标题等图表元素。
    // chart_new->addSeries(series_new);
    // chart_new->setTitle("1通道电流");



}

void Data_chart::data_chart()
{
    powerdata.P_CH1 = powerdata.C_CH1*powerdata.V_CH1;
    powerdata.P_CH2 = powerdata.C_CH2*powerdata.V_CH1;
    powerdata.P_CH3 = powerdata.C_CH3*powerdata.V_CH1;
    powerdata.P_CH4 = powerdata.C_CH4*powerdata.V_CH1;

    // powerdata.P_CH1 = sin(a)+2;
    // powerdata.P_CH2 = sin(a)+4;
    // powerdata.P_CH3 = sin(a)+6;
    // powerdata.P_CH4 = sin(a)+8;
    series1->append(a, (qreal)powerdata.P_CH1);
    series2->append(a, (qreal)powerdata.P_CH2);
    series3->append(a, (qreal)powerdata.P_CH3);
    series4->append(a, (qreal)powerdata.P_CH4);
    // 动态调整坐标轴范围
    if(clear_flag == 1)
    {
        axisX->setRange(0, 5);
        clear_flag = 0;
    }
    else
    {
        if (a > axisX->max()) {
            axisX->setRange(0, a + 0.1);  // 扩展 X 轴范围
        }
    }
    if(powerdata.P_CH1 > MAX_Y)
        MAX_Y = powerdata.P_CH1;
    if(powerdata.P_CH2 > MAX_Y)
        MAX_Y = powerdata.P_CH2;
    if(powerdata.P_CH3 > MAX_Y)
        MAX_Y = powerdata.P_CH3;

    if(powerdata.P_CH4 > MAX_Y)
        MAX_Y = powerdata.P_CH4;

    if(MAX_Y > axisY->max()) {
        axisY->setRange(0, (qreal)MAX_Y + 2);  // 扩展 Y 轴范围
    }
    // qDebug()<<"x= "<<x;
    // qDebug()<<"y= "<<y;
    a+=1;
}
void Data_chart::data_handle()
{

}
